// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-06-11 17:40:12
// 生成路径: internal/app/app/logic/app_user.go
// 生成人：yxfmlbb
// desc:用户表
// company:云南奇讯科技有限公司
// ==========================================================================

package appUser

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/tiger1103/gfast/v3/internal/app/app/dao"
	"github.com/tiger1103/gfast/v3/internal/app/app/model"
	"github.com/tiger1103/gfast/v3/internal/app/app/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/app/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterAppUser(New())
}

func New() service.IAppUser {
	return &sAppUser{}
}

type sAppUser struct{}

func (s *sAppUser) List(ctx context.Context, req *model.AppUserSearchReq) (listRes *model.AppUserSearchRes, err error) {
	listRes = new(model.AppUserSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.AppUser.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.AppUser.Columns().Id+" = ?", req.Id)
		}
		if req.Username != "" {
			m = m.Where(dao.AppUser.Columns().Username+" like ?", "%"+req.Username+"%")
		}
		if req.Nickname != "" {
			m = m.Where(dao.AppUser.Columns().Nickname+" like ?", "%"+req.Nickname+"%")
		}
		if req.Password != "" {
			m = m.Where(dao.AppUser.Columns().Password+" like ?", "%"+req.Password+"%")
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.AppUserListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.AppUserListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.AppUserListRes{
				Id:        v.Id,
				Username:  v.Username,
				Nickname:  v.Nickname,
				Password:  v.Password,
				CreatedAt: v.CreatedAt,
				Status:    v.Status,
				UserType:  v.UserType,
				Age:       v.Age,
				Gender:    v.Gender,
				Salt:      v.Salt,
				Mobile:    v.Mobile,
			}
		}
	})
	return
}

func (s *sAppUser) GetById(ctx context.Context, id uint64) (res *model.AppUserInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.AppUser.Ctx(ctx).WithAll().Where(dao.AppUser.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
		if res != nil {
		}
	})
	return
}

func (s *sAppUser) Add(ctx context.Context, req *model.AppUserAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppUser.Ctx(ctx).Insert(do.AppUser{
			Username: req.Username,
			Nickname: req.Nickname,
			Password: req.Password,
			Status:   req.Status,
			UserType: req.UserType,
			Age:      req.Age,
			Gender:   req.Gender,
			Salt:     req.Salt,
			Mobile:   req.Mobile,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sAppUser) Edit(ctx context.Context, req *model.AppUserEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppUser.Ctx(ctx).WherePri(req.Id).Update(do.AppUser{
			Username: req.Username,
			Nickname: req.Nickname,
			Password: req.Password,
			Status:   req.Status,
			UserType: req.UserType,
			Age:      req.Age,
			Gender:   req.Gender,
			Salt:     req.Salt,
			Mobile:   req.Mobile,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sAppUser) Delete(ctx context.Context, ids []uint64) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppUser.Ctx(ctx).Delete(dao.AppUser.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
